require "Common/define"
require "Logic/Config"
require "Common/functions"

EcoBuffManager = {};
local this = EcoBuffManager;
local buffTableData;
this.buffList = {};

function EcoBuffManager.init()
    buffTableData = G.dataTable["t_ecobuff"];
end

function EcoBuffManager.OnDestroy()
    this.buffList = {};
end

function EcoBuffManager.getEcoBuffTableDataById(id)
    return buffTableData[tostring(id)];
end

function EcoBuffManager.addOneClassBuff(type, data)
    if data == nil then
        return;
    end
    for k, v in pairs(data) do
        this.oneBuffData = this.getEcoBuffTableDataById(v);
        if this.oneBuffData ~= nil then
            this.effectValue = decodeJsonStrToTabel(this.oneBuffData.f_BuffEffectValue);
            if this.buffList[type][this.oneBuffData.f_BuffType] == nil and #this.effectValue > 1 then
                this.buffList[type][this.oneBuffData.f_BuffType] = {};
            end
            if #this.effectValue == 1 then
                this.buffNum = this.buffList[type][this.oneBuffData.f_BuffType] or 0;
                this.buffNum = this.buffNum + this.effectValue[1];
                this.buffList[type][this.oneBuffData.f_BuffType] = this.buffNum;
            elseif #this.effectValue == 2 then
                this.buffNum = this.buffList[type][this.oneBuffData.f_BuffType][this.effectValue[1]] or 0;
                this.buffNum = this.buffNum + this.effectValue[2];
                this.buffList[type][this.oneBuffData.f_BuffType][this.effectValue[1]] = this.buffNum;
            elseif #this.effectValue == 3 then
                if this.buffList[type][this.oneBuffData.f_BuffType][this.effectValue[1]] == nil then
                    this.buffList[type][this.oneBuffData.f_BuffType][this.effectValue[1]] = {};
                end
                table.insert(this.buffList[type][this.oneBuffData.f_BuffType][this.effectValue[1]], {this.effectValue[2], this.effectValue[3]});
            end
        end
    end
end

function EcoBuffManager.updateEcoBuff(type, data)
    if type == nil or data == nil then
        return;
    end
    this.buffList[tostring(type)] = {};
    this.addOneClassBuff(tostring(type), data);
end

function EcoBuffManager.getEcoBuffByTypeAndItemId(type1, id)
    if id == nil then
        local sum = 0;
        for k, v in pairs(this.buffList) do
            if v[type1] ~= nil then
                sum = sum + v[type1];
            end
        end
        return sum;
    end
    local sum = 0;
    local tableData = {};
    local flag = false;
    for k, v in pairs(this.buffList) do
        if v[type1] ~= nil and v[type1][id] ~= nil then
            if type(v[type1][id]) == "table" then
                flag = true;
                for key, value in pairs(v[type1][id]) do
                    table.insert(tableData, value);
                end
            else
                sum = sum + v[type1][id];
            end
        end
    end
    if flag then
        return tableData;
    else
        return sum;
    end
end

return EcoBuffManager